package com.channel.temperature.collect.ui.activity

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.annotation.RequiresApi
import com.github.gzuliyujiang.wheelpicker.DatimePicker
import com.github.gzuliyujiang.wheelpicker.annotation.DateMode
import com.github.gzuliyujiang.wheelpicker.annotation.TimeMode
import com.github.gzuliyujiang.wheelpicker.entity.DatimeEntity
import com.hjq.toast.ToastUtils
import com.channel.temperature.collect.app.base.BaseActivity
import com.channel.temperature.collect.app.ext.initBack
import com.channel.temperature.collect.app.util.MyUtil
import com.channel.temperature.collect.databinding.ActivityAddTaskBinding
import com.channel.temperature.collect.ui.viewmodel.AddTaskModel
import me.hgj.mvvmhelper.ext.setOnclickNoRepeat
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.Date


class AddTaskActivity : BaseActivity<AddTaskModel, ActivityAddTaskBinding>() {
    companion object {
        const val TAG = "AddTaskActivity"
    }

    var time = ""
    var delay = 0
    var position = 0


    @RequiresApi(Build.VERSION_CODES.TIRAMISU)
    override fun initView(savedInstanceState: Bundle?) {
        //初始化toolbar
        mToolbar.initBack("编辑任务") {
            finish()
        }
        time = intent.getStringExtra("time").toString()
        delay = intent.getIntExtra("interval", 0)
        position = intent.getIntExtra("position", 0)
        if (!checkTime(time)) {
            mBind.collectLayout.visibility = View.GONE
            mBind.collectLayoutDivide.visibility = View.GONE
            mBind.pressureLayoutDivide.visibility = View.VISIBLE
            mBind.pressureLayout.visibility = View.VISIBLE
            mBind.pressureET.setText(time)
            mBind.collectJGTimeET.setText("" + delay)
        } else {
            mBind.collectLayout.visibility = View.VISIBLE
            mBind.pressureLayout.visibility = View.GONE
            mBind.collectLayoutDivide.visibility = View.VISIBLE
            mBind.pressureLayoutDivide.visibility = View.GONE
            mBind.collectTimeET.text = MyUtil.DateToStr(MyUtil.StrToDate(time))
            mBind.collectJGTimeET.setText("" + delay)
        }

    }

    //是正确时间 那就是采集参数 否则就是压力参数
    fun checkTime(str: String): Boolean {
        var isTime = true
        val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        var date: Date? = null
        try {
            date = format.parse(str)
        } catch (e: ParseException) {
            e.printStackTrace()
            isTime = false
        }
        return isTime
    }

    override fun onBindViewClick() {
        //绑定点击事件
        setOnclickNoRepeat(
            mBind.saveTask, mBind.collectTimeET
        ) {
            when (it) {

                mBind.saveTask -> {
                    if (checkCanSave()) {
                        var mTime = ""
                        if (!checkTime(time)) {
                            mTime = mBind.pressureET.text.toString().trim()
                        } else {
                            mTime = mBind.collectTimeET.text.toString().trim()
                        }

                        var mInterval = mBind.collectJGTimeET.text.toString().trim().toInt()
                        val i = Intent()
                        i.putExtra("index", position)
                        i.putExtra("time", mTime)
                        i.putExtra("delay", mInterval)
                        setResult(RESULT_OK, i)
                        AddTaskActivity@ this.finish()
                    }

                }

                mBind.collectTimeET -> {
                    var picker = DatimePicker(this)
                    var wheelLayout = picker.wheelLayout
                    picker.setOnDatimePickedListener { year, month, day, hour, minute, second ->
                        var text = "$year-$month-$day $hour:$minute:$second"
                        mBind.collectTimeET.text = MyUtil.DateToStr(MyUtil.StrToDate(text))
                    }
                    wheelLayout.setDateMode(DateMode.YEAR_MONTH_DAY)
                    wheelLayout.setTimeMode(TimeMode.HOUR_24_NO_SECOND)
                    wheelLayout.setRange(DatimeEntity.now(), DatimeEntity.yearOnFuture(10))
                    wheelLayout.setDateLabel("年", "月", "日")
                    wheelLayout.setTimeLabel("时", "分", "秒")
                    picker.show()
                }
            }
        }
    }

    fun checkCanSave(): Boolean {
        if (!checkTime(time)) {
            if (mBind.pressureET.text.toString().trim()
                    .isNullOrEmpty() || mBind.collectJGTimeET.text.toString().trim()
                    .isNullOrEmpty()
            ) {
                ToastUtils.show("请填写完整的信息")
                return false
            } else {
                return true
            }
        } else {
            if (mBind.collectTimeET.text.toString().trim()
                    .isNullOrEmpty() || mBind.collectJGTimeET.text.toString().trim()
                    .isNullOrEmpty()
            ) {
                ToastUtils.show("请填写完整的工作任务信息")
                return false
            } else {
                return true
            }
        }

    }

    override fun onRequestSuccess() {

    }


    override fun onDestroy() {

        super.onDestroy()
    }

}